package com.queming.core.service.file;

import com.queming.core.Utils.FileTypeFromExt;
import com.queming.core.dto.FileDto;
import com.queming.core.dto.FileUploadDto;
import com.queming.core.dto.FileUploadRequest;
import com.queming.core.entity.File;
import com.queming.core.enums.FileStatus;
import com.queming.core.enums.Storage;
import com.queming.core.exception.BizException;
import com.queming.core.exception.ExceptionType;
import com.queming.core.mapper.FileMapper;
import com.queming.core.respository.FileRepository;
import com.queming.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author chencidiao
 * @date 2022/05/04 13:45
 **/
@Service
public class FileServiceImpl extends BaseService implements FileService{

    private Map<String,StorageService> storageService;
    @Autowired
    public void setStorageService(Map<String, StorageService> storageService) {
        this.storageService = storageService;
    }

    private FileRepository repository;
    @Autowired
    public void setRepository(FileRepository repository) {
        this.repository = repository;
    }

    private FileMapper mapper;
    @Autowired
    public void setMapper(FileMapper mapper) {
        this.mapper = mapper;
    }

    /**
     *
     * @author 陈词调
     * @date 2022/5/4
     * @Transactional注解在代码执行出错的时候能够进行事务的回滚。
     */
    @Override
    @Transactional
    public FileUploadDto initUpload(FileUploadRequest fileUploadRequest) throws IOException {
        File file = mapper.createEntity(fileUploadRequest);
        file.setType(FileTypeFromExt.getFileTypeFromExt(fileUploadRequest.getExt()));
        file.setStorage(getDefaultStorage());
        file.setCreatedBy(getCurrentUserEntity());
        file.setUpdatedBy(getCurrentUserEntity());
        File saveFile = repository.save(file);
        //获取sts令牌
        FileUploadDto fileUploadDto = storageService.get(getDefaultStorage().name()).initFileUpload();
        fileUploadDto.setKey(saveFile.getKey());
        fileUploadDto.setFileId(saveFile.getId());
        return fileUploadDto;
    }

    @Override
    public FileDto finishUpload(String id) {
        File file = getFileEntity(id);

        //你不是创建者的话就无权更改
        if (!Objects.equals(file.getCreatedBy().getId(),getCurrentUserEntity().getId())){
            throw new BizException(ExceptionType.FILE_NOT_PERMISSION);
        }
        file.setStatus(FileStatus.UPLOADED);
        return mapper.toDto(repository.save(file));
    }

    @Override
    public File getFileEntity(String id) {
        Optional<File> fileOptional=repository.findById(id);
        if (!fileOptional.isPresent()){
            throw new BizException(ExceptionType.FILE_NOT_FOUND);
        }
        return fileOptional.get();
    }

    /**
     *
     * @author 陈词调
     * @date 2022/5/4
     *后台设置当前storage
     */

    @Override
    public Storage getDefaultStorage(){
        return Storage.COS;
    }
}
